package com.das.mechanic_base.adapter.common;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.main.CarBrandBean;
import com.das.mechanic_base.bean.main.CarModelSelectBean;
import com.das.mechanic_base.utils.X3StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class X3SelectBrandNewAdapter extends RecyclerView.Adapter<X3SelectBrandNewAdapter.SelectStyleHolder> {

    private List<CarBrandBean> mList;

    private Context mContext;

    private boolean isName;
    //在品牌界面搜索车款
    private boolean isSearchStyle;
    private List<CarModelSelectBean> selectLists;
    private List<CarModelSelectBean> pList;

    private HashMap<String,String> brandMap;

    boolean isFilter;

    public X3SelectBrandNewAdapter(Context mContext, List<CarModelSelectBean> selectLists) {
        this.mContext = mContext;
        this.mList = new ArrayList<>();
        this.pList = new ArrayList<>();
        this.selectLists = selectLists;
        this.brandMap = new HashMap<>();
    }

    public boolean isFilter() {
        return isFilter;
    }

    public void setFilter(boolean filter) {
        isFilter = filter;
    }

    public boolean isSearchStyle() {
        return isSearchStyle;
    }

    public void setSearchStyle(boolean searchStyle) {
        isSearchStyle = searchStyle;
    }

    public void changeData(List<CarBrandBean> mList, boolean isName) {
        this.mList.clear();
        this.mList.addAll(mList);
        this.isName = isName;
        this.pList.clear();
        this.brandMap.clear();

        if(!X3StringUtils.isListEmpty(selectLists)){
            for (int i = 0; i < selectLists.size(); i++){
                CarModelSelectBean carBean = selectLists.get(i);
                if (brandMap.containsKey(carBean.brandName)) {
                    int num =  Integer.parseInt(brandMap.get(carBean.brandName)) + 1;
                    brandMap.put(carBean.brandName,("ALL".equals(carBean.modelValue)) ? mContext.getResources().getString(R.string.all) : (num >= 100) ? "99+" : num + "");
                }else {
                    brandMap.put(carBean.brandName,("ALL".equals(carBean.modelValue)) ? mContext.getResources().getString(R.string.all) : "1");
                }
            }
        }

        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public SelectStyleHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new SelectStyleHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_select_brand_new_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull SelectStyleHolder holder, int position) {
        //crm模块 选中的个数
        holder.tv_num.setText(brandMap.get(mList.get(position).getLabel()));
        holder.tv_num.setVisibility(X3StringUtils.isEmpty(brandMap.get(mList.get(position).getLabel())) ? View.GONE : View.VISIBLE);


        String label = mList.get(position).getLabel();

        if (isFilter){
                String content =  label.replaceAll("<em>", "<font color='#0077FF'>").replaceAll("</em>", "</font>");

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    holder.tv_name.setText(Html.fromHtml(content,0));
                }else {
                    holder.tv_name.setText(Html.fromHtml(content));
                }
        } else {
            holder.tv_name.setText(mList.get(position).getLabel() + "");
        }

        if (isName || isSearchStyle) {
            holder.iv_icon.setVisibility(View.GONE);
        }else {
            CarBrandBean.KwParamBean kwParam = mList.get(position).getKwParam();
            if (kwParam != null) {
                holder.iv_icon.setVisibility(View.VISIBLE);
                String resourceUrl = kwParam.getResourceUrl();
                Glide.with(mContext).load(X3StringUtils.getImageUrl(resourceUrl)).apply(
                        new RequestOptions().placeholder(R.mipmap.x3_car_icon_no).skipMemoryCache(false).dontAnimate()
                ).into(holder.iv_icon);
            }else {
                holder.iv_icon.setVisibility(View.INVISIBLE);
            }
        }

        holder.itemView.setOnClickListener(view -> {
            if (iOnSelectStyle != null) {
                iOnSelectStyle.iOnSelectStyleName(mList.get(position),isSearchStyle);
            }
        });
    }

    public boolean isGroupHead(int position) {
        if (position >= mList.size()) {
            return false;
        }
        if (position == 0) {
            return true;
        } else {
            String currentGroup = getGroupName(position);
            String preGroup = getGroupName(position - 1);
            return !currentGroup.equals(preGroup);
        }
    }

    /**
     * 获取组名
     */
    public String getGroupName(int position){

        String sortLetters = mList.get(position).getSortLetters();

        return "🔥".equals(sortLetters) ? mContext.getString(R.string.x3_common_brand) : sortLetters;
    }

    IOnSelectStyle iOnSelectStyle;

    public void setiOnSelectStyle(IOnSelectStyle iOnSelectStyle) {
        this.iOnSelectStyle = iOnSelectStyle;
    }

    public interface IOnSelectStyle {
        void iOnSelectStyleName(CarBrandBean name,boolean isSearchStyle);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class SelectStyleHolder extends RecyclerView.ViewHolder {
        TextView tv_name;
        ImageView iv_icon;
        TextView tv_num;
        public SelectStyleHolder(@NonNull View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
            iv_icon = itemView.findViewById(R.id.iv_icon);
            tv_num = itemView.findViewById(R.id.tv_num);
        }
    }
}
